//
//  BGAddReportViewCell.m
//  bigMall
//
//  Created by LTOVE on 16/9/23.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGAddReportViewCell.h"
#import "BGAddReportModel.h"
#import "BGcollectionViews.h"

#import "MJExtension.h"

static NSString *const reuseIdentify = @"cell";

@interface BGAddReportViewCell ()<BGcollectionViewsDelegate,UITextFieldDelegate>

@end

@implementation BGAddReportViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // Configure the view for the selected state
}

- (void)setModel:(BGAddReportModel *)model
{
    _model = model;
    [self setUpAllSubViewsWithModel:model];
}
/**
 *  添加所有子空间
 *
 *  @param model model description
 */
- (void)setUpAllSubViewsWithModel:(BGAddReportModel *)model
{
    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    UIView *contentView = [[UIView alloc]initWithFrame:self.bounds];
    if ([model isKindOfClass:[BGAddReportModelLineCell class]]) {
        //cell分割线
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenW, 10)];
        lineView.backgroundColor = lightGray_;
        [contentView addSubview:lineView];
    }else if ([model isKindOfClass:[BGAddReportModelTitle class]]){
        //质检报告的标题
        BGAddReportModelTitle *itemModel = (BGAddReportModelTitle *)model;
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = itemModel.title;
        titleLabel.font = Font34;
        titleLabel.textColor = textBlackColor;
        [titleLabel sizeToFit];
        titleLabel.x = BGMaginLeft;
        titleLabel.centerY = contentView.height * 0.5;
        [contentView addSubview:titleLabel];
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(BGMaginLeft, contentView.height - 1, screenW - 2 * BGMaginLeft, 1)];
        lineView.backgroundColor = lightGray_;
        [contentView addSubview:lineView];
    }else if ([model isKindOfClass:[BGAddReportModelLists class]]){
        //列表
        BGAddReportModelLists *itemModel = (BGAddReportModelLists *)model;
        UILabel *titleLabel = [UILabel new];
        titleLabel.text = [NSString stringWithFormat:@"%@:",itemModel.title];
        titleLabel.font = Font34;
        titleLabel.textColor = textBlackColor;
        [titleLabel sizeToFit];
        titleLabel.x = BGMaginLeft;
        titleLabel.centerY = contentView.height * 0.5;
        [contentView addSubview:titleLabel];
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(BGMaginLeft, contentView.height - 1, screenW - 2 * BGMaginLeft, 1)];
        lineView.backgroundColor = lightGray_;
        [contentView addSubview:lineView];
        UITextField *textFild = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(titleLabel.frame), titleLabel.y, screenW - 2 * BGMaginLeft - titleLabel.width, titleLabel.height)];
        textFild.font = titleLabel.font;
        textFild.textColor = itemModel.color;
        textFild.text = itemModel.valueStr;
        textFild.keyboardType = UIKeyboardTypeDecimalPad;
        textFild.delegate = self;
        [contentView addSubview:textFild];
    }else if ([model isKindOfClass:[BGAddReportModelImages class]]){
        //图片
        BGAddReportModelImages *itemModel = (BGAddReportModelImages *)model;
        BGcollectionViews *colluetionView = [[BGcollectionViews alloc]init];
        colluetionView.backgroundColor = lightwhite_;
        colluetionView.Mydelegate = self;
        [colluetionView registerClass:[collectionViewCells class] forCellWithReuseIdentifier:@"cell"];
        colluetionView.dataArray = itemModel.photosArray;
        [contentView addSubview:colluetionView];
    }
    [self.contentView addSubview:contentView];
}
#pragma mark -- 文本填写完成

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    BGAddReportModelLists *itemModel = (BGAddReportModelLists *)self.model;
    itemModel.valueStr = textField.text;
    
    if ([_delegate respondsToSelector:@selector(textFildDidEndEditingWith:)]) {
        [_delegate textFildDidEndEditingWith:itemModel];
    }
    
//    GWLog(@"%@",itemModel.keyValues);
}

/**
 *  最后一个选择了
 */
- (void)lastItemsCellSelected
{
    if ([_delegate respondsToSelector:@selector(lastCollectionItemClick)]) {
        [_delegate lastCollectionItemClick];
    }
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    BGAddReportViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentify];
    }
    return cell;
}

@end
